<?php


/*
 * @license   GPLv3 Open Source
 */


defined ('IN_AGORA') or die;

class TaskController extends AgoraAdminController
{

	function loadModel() {
		$this->model = Model::getInstance('JoopquestionsModel');
	}	

 	function _default()
	{
		$this->view->template = 'joopport/questions';
		$this->setPagination($this->model->loadAllCount());
		$questions = $this->model->loadAll('',$this->pagination->per_page,$this->pagination->page);
		
		$this->view->assignRef('questions',$questions);
		
		
	}

	function add_question() {
		$this->view->template = 'joopport/add_question';
	}
	
	function save() {
		$form = Agora::getPostVar('form');
		
		$form['subject'] = trim($form['subject']);
		if (!isset($form['subject']) or empty($form['subject'])) {
			Agora::showError("Subject can't be empty");
			if (!isset($form['id'])){
				$this->view->template = 'joopport/add_question';
			}
			else {
				$licenses = $this->model->load($form['id']);
				$this->view->assignRef('questions',$questions);
				$this->view->template = 'joopport/edit_question';
			}
			return;
		}
		
		if (isset($form['id'])) {
			$this->model->edit($form['id'],$form);

		} else {
			$this->model->add($form);
		}

		$this->redirect();		
	}

	function edit()
	{
		$cid = Agora::getRequestVar('cid');

		if (is_array($cid)) {
			$id = array_shift($cid);
		} else {
			$id = $cid;
		}
		
		
		
		if ($this->model->exists($id,'id')){

			$questions = $this->model->load($id);
	
			$this->view->assignRef('questions',$questions);
			$this->view->template = 'joopport/edit_question';
		}
		else {
			$this->redirect();
		}
	}
	
	function cancel() {
		$this->redirect();
	}
	
	function remove() {
		$cid = Agora::getPostVar('cid');

		foreach ($cid as $id) {
			$this->model->delete($id);
			$this->model->delete_kit(0, $id); //удаляем привязку к группам
		}
		$this->redirect();
	}
	
}